package domain.searchEngine.origin;

import persistence.CorruptDataException ;
import persistence.OriginIndex ;
import util.Log ;
import util.Subsystem ;
import domain.ForumData ;
import domain.searchEngine.IndexAgent ;
import domain.searchEngine.SearchEngine ;
import domain.searchEngine.SearchEngineFactory ;

/**
 * builds components for searching using the original search engine.
 *
 * @author idan
 */
public class OriginSearchEngineFactory implements SearchEngineFactory
{
	private OriginIndex index;
	private ForumData forumData ;

	/**
	 * @param indexUri URI of the index file.
	 * @param forumData data from which search results are retrieved.
	 */
	public OriginSearchEngineFactory(final String indexUri, final ForumData forumData)
	{
		this.index = new OriginIndex(indexUri) ;
		try
		{
			this.index = this.index.loadIndex(indexUri); //load data from persistence
		}
		catch (CorruptDataException e)
		{
			Log.getLogger(Subsystem.SEARCH_ENGINE).info("the index is corrupt and will be cleared.") ;
		}
		this.forumData = forumData ;
	}

	/**
	 * @see domain.searchEngine.SearchEngineFactory#getIndexAgent()
	 */
	public IndexAgent getIndexAgent()
	{
		return new OriginIndexAgent(this.index) ;
	}

	/**
	 * @see domain.searchEngine.SearchEngineFactory#getSearchEngine()
	 */
	public SearchEngine getSearchEngine(){
		return new OriginSearchEngine(this.index, this.forumData) ;
	}

}
